<?php

/**
 * manage data of product table
 *
 * @author tuancasi
 * 
 * properties of this table
 * @property int $product_id 
 * @property int $user_id 
 * @property string $name
 * @property string $image
 * @property float $price
 * @property float $sale
 * @property string $more_info
 * @property int $category_id
 * @property int $stock
 * @property int $description
 * @property int $create_time
 * @property int $update_time
 */
class Product extends CActiveRecord {

    const TOTAL_RECORD = 24;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return User the static model class
     */
    public static function model($className = __CLASS__) {
	return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
	return '{{products}}';
    }

    public function rules() {
	return array(
	    array('name, image, price', 'required', 'on' => 'insert'),
	    array('name, image, price', 'safe', 'on' => 'update'),
	    array('price', 'numerical'),
	    array('product_id, sale, more_info, category_id, stock, description, create_time, update_time', 'safe')
	);
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
	// NOTE: you may need to adjust the relation name and the related
	// class name for the relations automatically generated below.
	return array(
	    'category' => array(self::BELONGS_TO, 'Category', 'category_id')
	);
    }

    /**
     * 
     * 
     * @param int $type is comment or post
     * @return \CActiveDataProvider
     */
    public function search() {
	// Warning: Please modify the following code to remove attributes that
	// should not be searched.

	$criteria = new CDbCriteria;

	$criteria->compare('name', $this->name, true);
	$criteria->compare('category_id', $this->category_id);
	return new CActiveDataProvider($this, array(
	    'criteria' => $criteria,
	    'pagination' => array(
		'pageSize' => param('pageSize')
	    ),
	    'sort' => array(
		'defaultOrder' => array(
		    't.time_created' => CSort::SORT_DESC
		)
	    )
	));
    }

    public function getImg($size = '') {
	switch ($size) {
	    case 'thumb' :
		$width = 150;
		$height = 210;
		break;
	    case 'small' :
		$width = 64;
		$height = 64;
		break;
	    case 'large':
		$width = 188;
		$height = 348;
		break;
	    default :
		return PRODUCT_URL . $this->image;
	}
	return ImageHelper::thumb($width, $height, 'product' . DIRECTORY_SEPARATOR . $this->image);
    }

    public function afterDelete() {
	parent::afterDelete();
	if ($this->image) {
	    deleteFile(PRODUCT_PATH . $this->image);
	}
    }

    public function getPrice() {
	$price = floatval($this->price);
	if ($this->sale) {
	    $sale = floatval($this->sale);
	    $priceAfterSale = floatval($price - ($price * floatval($sale / 100)));
	    return "<span class='price-before-sale'>$" . $price . "</span><span class='price-after-sale'>$" . $priceAfterSale . "</span>";
	} else {
	    return "<span class='price-after-sale'>$" . $price . "</span>";
	}
    }

}

